<?php
/*
 * 推荐位内容管理控制器
 * @auth 索索
 */

namespace Admin\Controller;
use Think\Controller;

class PositionContentController extends CommonController {
	
	public function index (){
		$conds = array();
		$title = $_GET['title'];
		if($title){
			$conds['title'] = $title;
			$this->assign('title',$conds['title']);
		}
		if($_GET['position_id']){
			$conds['position_id'] = intval($_GET['position_id']);
			$this->assign('position_id',$conds['position_id']);
		}
		
		
		 $positionnames = D('Position')->getPositionNames();
		 $conds['status'] = array('neq',-1);
		 $page = $_REQUEST['p'] ? $_REQUEST['p']:1;
		 $pageSize = $_REQUEST['pageSize'] ?$_REQUEST['pageSize'] : 8;
		 $positionsContent = D("PositionContent")->getPositionContent($conds,$page,$pageSize);
		 $positionsContentCount = D("PositionContent")->getPositionContentCount($conds);
		 
		 $res = new \Think\Page($positionsContentCount,$pageSize);
		 $pageRes = $res->show();
//		 print_r($pageRes);
		 $this->assign('positionnames',$positionnames);
		 $this->assign('pageRes',$pageRes);
		 $this->assign('positionsContent',$positionsContent);
		 $this->display();
	}
	
	public function add (){
		
		if($_POST){
			if(!$_POST['position_id'] || $_POST['position_id']==''){
		 		return show(0, '推荐位id不存在');
			}
			if(!isset($_POST['title']) || !$_POST['title'] ){
		 		return show(0, '推荐标题不存在');
			}
			if(!isset($_POST['url']) || !$_POST['url']){//链接不存在
				if(!isset($_POST['news_id']) || !$_POST['news_id']){
					return show(0, '推荐链接和文章id必填一个');
				}
			}
			if($_POST['news_id']){
				$news = D('News')->getNewsById($_POST['news_id']);
				$thumb = $news['thumb'];
			}
			if(!isset($_POST['thumb']) || !$_POST['thumb']){//未上传图片
		 		if($thumb && !$thumb==""){//文章缩略图存在
			 		$_POST['thumb'] = $thumb;
		 		}
		 		if(!$thumb){
		 			return show(0, '推荐位图片不存在');
		 		}	
			}
			if($_POST['id']){//有id更新保存
				return $this->save($_POST);
			}
			
			$positionContentId = D('PositionContent')->insert($_POST);
			if($positionContentId){
				return show(1,"新增成功");
			}else{
				return show(0,"新增失败");
			}
		}else{
			$positionnames = D('Position')->getPositionNames();
			$this->assign('positionnames',$positionnames);
			$this->display();
		}
		
	}
	
	public function del (){
		try{
			if($_POST){
				$id=$_POST['id'];
				$status=$_POST['status'];
				$res=D('PositionContent')->updataStatusById($id,$status);
				if($res){
					return show(1, '操作成功！');
				}else{
					return show(0, '操作失败！');
				}
			}
		}catch(Exception $e){
			return show(0, $e->getMessage());
		}
		return show(0, '没有提交的数据');
	}
	
	public function save ($data) {
		$positionId = $data['id'];
		unset($data['id']);
		try{
			$id=D("PositionContent")->updataPositionConById($positionId,$data);
			if($id===false){
				return show(0, '更新失败');
			}
			return show(1, '更新成功');
		}catch(Exception $e){
			return show(0, $e->getMessage());
		}
	}
	
	public function edit () {
		$positionConId = $_GET['id'];
		if(!$positionConId){
			//推荐位内容id不存在执行跳转
			$this ->rediret('/admin.php?c=position');
		}
		$positionCon = D('PositionContent')->getPositionConById($positionConId);
		if(!$positionCon){
			//推荐位内容不存在执行跳转
			$this ->rediret('/admin.php?c=position');
		}
		$positionnames = D('Position')->getPositionNames();
		$this->assign('positionnames',$positionnames);
		$this->assign('positionCon',$positionCon);
		$this->display();
	}
	
	public function listorder () {
		$listoder=$_POST['listorder'];
		$jumpUrl=$_SERVER['HTTP_REFERER'];
		$errors=array();
		if($listoder){
			try{
			foreach($listoder as $PcId=>$v){
				$id=D('PositionContent')->updataListorderById($PcId,$v);
				if($id===false){
					$errors[]=$PcId;
				}
			}
			}catch(Exception $e){
				return show(0, $e->getMessage(),array('jump_url'=>$jumpUrl));
			}
			if($errors){
				return show(0, '排序失败-'.implode(',', $errors),array('jump_url'=>$jumpUrl));
			}
			return show(1, '排序成功',array('jump_url'=>$jumpUrl));
		}
		return show(0, '排序数据失败',array('jump_url'=>$jumpUrl));
	}
} 
 
